package com.timedomain.him.remake.yggdrasil;

import android.content.Context;
import android.text.TextUtils;

import com.timedomain.him.remake.Program;
import com.timedomain.him.remake.utils.FileUtils;
import com.timedomain.him.remake.utils.HLog;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class WorldLoader {
    public static HashMap<String,TreeLeaf> load(Context ctx){
        String rootPath = FileUtils.pathCombine("chat","data");
        HashMap<String,TreeLeaf> results = new HashMap<>();
        enumurateDirectories(results,(rootPath),ctx);
        return results;
    }

    private static void enumurateDirectories(HashMap<String,TreeLeaf> accesser,String root,Context ctx){
        List<String> files = FileUtils.listFiles(root);
        for (String file :
                files) {
            if(file.toLowerCase().endsWith(".txt")){
                doOnFile(accesser,file,ctx);
            }

        }
    }

    private static void doOnFile(HashMap<String,TreeLeaf> accesser,String f,Context ctx){

        String currentLeafName = null;
        ArrayList<String> currentLeafBranches = null;

        try(BufferedReader br = new BufferedReader(new InputStreamReader(Program.openAsset(ctx,f), StandardCharsets.UTF_8))){
            String line = br.readLine();
            if(line == null){return;}
            do{
                if(line.trim().startsWith("#")){
                    continue;
                }
                if(TextUtils.isEmpty(line.trim())){
                    continue;
                }
                if(line.startsWith("\t") || line.startsWith("    ")){
                    currentLeafBranches.add(line.trim());
                }
                else if(line.startsWith("[") && line.trim().endsWith("]")){
                    if(currentLeafBranches != null && currentLeafName != null){
                        TreeLeaf leaf = new TreeLeaf();
                        leaf.tagName = currentLeafName;
                        leaf.branches = currentLeafBranches;
                        accesser.put(leaf.tagName, leaf);
                    }
                    currentLeafName = line.trim();
                    currentLeafBranches = new ArrayList<>();
                }
                else{
                    HLog.e("WorldTreeLoader", "doOnFile: Invalid line: "+line);
                }
            }
            while ((line = br.readLine()) != null);
            if(currentLeafBranches != null && currentLeafName != null){
                TreeLeaf leaf = new TreeLeaf();
                leaf.tagName = currentLeafName;
                leaf.branches = currentLeafBranches;
                accesser.put(leaf.tagName, leaf);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
